﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
//8.加载：通过AssetReference
public class Example1 : MonoBehaviour
{
    [Serializable]
    public class AssetReferenceMaterial : AssetReferenceT<Material>
    {
        public AssetReferenceMaterial(string guid) : base(guid) { }
    }

    public GameObject prefab;
    public AssetReference prefabRef;
    public AssetReference texRef;

    
    public AssetReferenceTexture texRef2;//自带的
    public AssetReferenceT<Material> matRef;//Inspector显示不出来，可以使用自定义的模型

    public AssetReferenceMaterial matRef2;//自定义

    public List<AssetReference> assetList;
    private void Start()
    {
        
    }
    private void OnGUI()
    {
        if (GUILayout.Button("Prefab 实例化"))
        {
            Instantiate(prefab);
        }
        if (GUILayout.Button("AssetReference Texture加载"))
        {
            texRef.LoadAssetAsync<Texture>().Completed += args =>
            {
                Debug.Log("加载" + args.Result.name);
            };
        }
        if (GUILayout.Button("AssetReferenceTexture  加载"))
        {
            texRef2.LoadAssetAsync().Completed += args =>
            {
                Debug.Log("加载" + args.Result.name);
            };
        }

        if (GUILayout.Button("自定义 AssetReferenceMaterial  加载"))
        {
            matRef2.LoadAssetAsync().Completed += args =>
            {
                Debug.Log("加载" + args.Result.name);
            };
        }


        if (GUILayout.Button("AssetReference GameObject实例化"))
        {
            var handle = prefabRef.InstantiateAsync();
            handle.Completed += Loaded;
        }
        
        
    }


    private void Loaded(AsyncOperationHandle<GameObject> obj)
    {
        var cube = obj.Result;
        Debug.Log(cube.name);

        cube.GetComponent<Renderer>().material.mainTexture = texRef.Asset as Texture;
    }


}
